<?php
namespace app\cms\model;
/* *
 * 字段管理功能
 * */
class field extends \app\base\model\AdminModel{
    protected $table ='cms_field';  //字段表

    //列表
    public function _select($where,$order = NULL,$field = NULL){
       return $this->where($where)->field($field)->order($order)->select();
    }

    //单个
    public function _find($condition){
        return $this->where($condition)->find();
    }


    //增加模型字段->编辑数据库表字段
    public function addField($data = array()){
        //获取当前模型信息
        $tables = model('module')->tables($data['mid']);
        //查询是否有重复字段
        $field = $this->_find(array('mid'=>$data['mid'],'fields'=>$data['fields']));
        if($field){
            return FALSE;
        }
        //字段类型
        $type = intval($data['type']);
        $leng = intval($data['flong']);// 计算表单长度
        switch ($type){
            case 2: //多行文本
                $type=' text CHARACTER SET utf8 DEFAULT NULL';
                break;
            case 3: //编辑器
                $type=' mediumtext CHARACTER SET utf8 DEFAULT NULL';
                break;
            default://1单行文本,2多行文本(<=250),4下拉列表,5单选框,6复选框
                if( $leng>255 || $leng<1 ){
                    $leng=255;
                } 
                $type= ' varchar('.$leng.') CHARACTER SET utf8 DEFAULT NULL'; //普通
        }
        $sql = "ALTER TABLE {pre}".$tables." ADD ".$data['fields'].$type;
        try{ //异常捕获
            $this->execute($sql);
        } catch( \Exception $e ){
            return FALSE;
        }
        $result = $this->data($data)->insert(); 
        if($result){
           return TRUE;
        }else{
           return FALSE;
        }
    }

    //编辑模型字段->数据库表字段
    public function fieldedit($id,$data = array()){
        $result = $this->_update($data,array('id'=>$id));
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }

    //删除字段->数据库字段
    public function delField($fid){
        $info = $this->_find(array('id' => $fid),'mid,fields');  //字段信息
        if($info){
            $tables = model('module')->tables($info['mid']);
            $sql = "alter table {pre}".$tables." DROP COLUMN ".$info['fields']; //执行删除操作
            try{
                $this->execute($sql);
            } catch( \Exception $e ){
                return '执行SQL语句有误';
            }
            $result = $this->where(array('id' => $fid))->delete();
            if($result){
                return FALSE;
            }else{
                return '删除字段失败';
            }
        }else{
            return '数据库字段不存在';
        }
    }
    //扩展模型表单的生成
    public function fromInput($mid,$update = FALSE,$input_value = array()) {
        $info = $this->where(array('mid'=>$mid))->field('name,fields,type,inputext,inputhold,default_val,inputcss,validform,ignores')->order('neworder asc,id asc')->select();
        $input = array();
        if($info){
            foreach ($info as $key => $rs) {
                if($rs['inputcss']){//表单样式
                    $inputcss[$key] = ' '.$rs['inputcss'];
                }
                if($rs['validform']){//表单验证
                    if($rs['ignores'] == 1){//非必须输入只验证格式
                        $ignore[$key] = 'ignore="ignore" ';
                    }
                    $validform[$key] = $ignore[$key].'datatype="'.$rs['validform'].'" errormsg="'.$rs['errormsg'].'" ';
                }
                if($rs['inputext']){//表单扩展
                    $inputext[$key] = htmlspecialchars_decode($rs['inputext']).' ';
                }
                if($rs['inputhold']){
                    $inputhold[$key] = htmlspecialchars_decode($rs['inputhold']);
                }
                if($update){//表单的值
                    $values[$key] = $input_value[$rs['fields']];
                }else{
                    $values[$key] = $rs['default_val'];
                }
                $inputname[$key] = 'id="'.$rs['fields'].'" name="'.$rs['fields'].'" ';
                $inputplus[$key] = $inputname[$key].$validform[$key].$inputext[$key];
                switch ($rs['type']) {
                    case 2: //多行
                        $inputstr[$key] = '<textarea class="textarea'.$inputcss[$key].'" '.$inputplus[$key].'type="text">'.$values[$key].'</textarea>';
                    break;
                    case 3: //编辑器
                        $inputstr[$key] = '<textarea class="textarea'.$inputcss[$key].'" '.$inputplus[$key].'type="text">'.htmlspecialchars_decode($values[$key]).'</textarea>';
                    break;
                    case 4: //下拉
                        $option ='<option value="">请选择选项</option>'; 
                        if($rs['default_val']){
                            $option_array = explode('|', $rs['default_val']);
                            foreach ($option_array as $value) {
                                $strarray = explode('#', $value);
                                $option .='<option value="'.$strarray[0].'"';
                                if($input_value[$rs['fields']] == $strarray[0]){
                                    $option .='selected="selected"';
                                }
                                $option .='>'.$strarray[1].'</option>'; 
                            } 
                        }
                        $inputstr[$key] = '<select class="select'.$inputcss[$key].'" '.$inputplus[$key].'>'.$option.'</select>'; 
                    break; 
                    case 5: //单选
                        if($rs['default_val']){
                            $option_array = explode('|', $rs['default_val']);
                            foreach ($option_array as $radiokey=>$value) {
                                $strarray = explode('#', $value);
                                if($input_value[$rs['fields']] == $strarray[0]){
                                    $checked[$radiokey] ='checked="checked" ';
                                }
                                $radio[$key][] ='<input class="radio'.$inputcss[$key].'" '.$checked[$radiokey].' value="'.$strarray[0].'" type="radio" name="'.$rs['fields'].'">'.$strarray[1]; 
                            }
                            $inputstr[$key] = implode('', $radio[$key]);  
                        }
                    break; 
                    case 6: //复选
                        if($rs['default_val']){
                            $option_array = explode('#', $rs['default_val']);
                            foreach ($option_array as $radiokey=>$value) {
                                $strarray = explode('|', $value);
                                if($input_value[$rs['fields']] == $strarray[0]){
                                    $checked[$radiokey] ='checked="checked" ';
                                }
                                $checkbox[$key][] ='<input class="checkbox'.$inputcss[$key].'" '.$checked[$radiokey].' value="'.$strarray[0].'" type="checkbox" name="'.$rs['fields'].'[]">'.$strarray[1]; 
                            }
                            $inputstr[$key] = implode('', $checkbox[$key]);  
                        }
                    break; 
                    default: //1单行
                        $inputstr[$key] = '<input class="input'.$inputcss[$key].'" '.$inputplus[$key].'type="text" value="'.$values[$key].'" />';
                    break;
                }
                $input[$key]['title'] = $rs['name'];
                $input[$key]['input'] = $inputstr[$key].$inputhold[$key];
            }
        }
        return $input; 
    }
}